package com.zhuchang.self_rpc.test.democoding;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author RenPu
 * @Date 2022/2/8 14:46
 * @Version 1.0
 * @Description: 实现NIO 零拷贝服务端
 **/
public class NewIOServer {

    public static void main(String[] args) throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(7001);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket socket = serverSocketChannel.socket();
        socket.bind(inetSocketAddress);

        //创建byteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4069);
        while (true){
            System.out.println("准备数据接受...........");
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readCount=0;
            while (-1!=readCount){
                try{
                    System.out.println("数据接口处理中.............");
                    readCount = socketChannel.read(byteBuffer);
                }catch (Exception e){
                    break;
                }
                byteBuffer.rewind(); //倒带position = 0 mark 作废
            }
        }



    }


}
